package com.cungmua.app.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.cungmua.app.R;
import com.cungmua.app.adapter.OrderHistoryAdapter;
import com.cungmua.app.base.BaseDrawerActivitys;
import com.cungmua.app.handler.OrderHistoryHandler;
import com.cungmua.app.model.Cart;
import com.cungmua.app.model.OrderHistory;
import com.cungmua.app.model.request.OrderHistoryRequest;
import com.cungmua.app.model.response.CancelOrderResponse;
import com.cungmua.app.model.response.OrderHistoryResponse;
import com.cungmua.app.model.response.Pager;
import com.cungmua.app.task.OrderHistoryTask;
import com.cungmua.app.utils.AppConfig;
import com.cungmua.app.view.EndlessScrollListener;

import eu.erikw.PullToRefreshListView;

/**
 * Created by kelvin.nguyen on 7/21/14.
 */
public class OrderHistoryActivity extends BaseDrawerActivitys implements OrderHistoryHandler{
    private PullToRefreshListView listOrders;
    private OrderHistoryAdapter mAdapter;
    private Pager pager;
    protected int currentPage = 1;
    private EndlessScrollListener endlessScrollListener = new EndlessDealListener();
    private ImageView icon_home;
    private Button icon_cart;
    private LinearLayout btnCart;

    @Override
    public int getLayoutResource() {
        return R.layout.activity_orders_history;
    }

    @Override
    public void loadControls(Bundle savedInstanceState) {
        listOrders = (PullToRefreshListView) findViewById(R.id.listOrders);

        mAdapter = new OrderHistoryAdapter(this,this);
        listOrders.setAdapter(mAdapter);
        listOrders.setDivider(null);
        listOrders.setDividerHeight(0);
        listOrders.setOnRefreshListener(new PullToRefreshListView.OnRefreshListener() {
            @Override
            public void onRefresh() {
                currentPage = 1;
                updateView();
            }
        });

        icon_home=(ImageView)findViewById(R.id.icon_home);
        icon_cart=(Button)findViewById(R.id.icon_cart);
        btnCart=(LinearLayout)findViewById(R.id.btnCart);


        Cart currentCart=getCurrentCart();
        icon_cart.setText(currentCart.getTotalQuantity()+"");

        icon_home.setOnClickListener(new ImageView.OnClickListener() {
            @Override
            public void onClick(View v) {
                showMenu();
            }
        });

        btnCart.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                goToYourCart();
            }
        });

    }
    @Override
    public void updateView() {
        super.updateView();
        OrderHistoryRequest request=new OrderHistoryRequest(getDeviceId(),getToken());

            request.setUserId(getCurrentUser().getId());
            request.setPage(currentPage);
            request.setVersion(AppConfig.getApiVersion());

        OrderHistoryTask task=new OrderHistoryTask(this,this);
        task.execute(request);

    }

    @Override
    public void onListOrderHistories(OrderHistoryResponse response) {
        listOrders.onRefreshComplete();
        if (response.getData() != null) {
            if (response.getData().size() > 0) {
                if (currentPage == 1) {
                    mAdapter.setDataSource(response.getData());
                    pager = response.getPager();
                    endlessScrollListener = new EndlessDealListener();
                    listOrders.setOnScrollListener(endlessScrollListener);
                } else {
                    mAdapter.addToDataSource(response.getData());
                }
            } else {
                listOrders.setEmptyView(findViewById(R.id.txtEmptyPastDeal));
            }
        }

    }

    @Override
    public void onButtonDestroyClicked(OrderHistory orderHistory) {

    }

    @Override
    public void onCancelSuccess(CancelOrderResponse response) {
        if(response.getCode()==0)
            updateView();
    }

    class EndlessDealListener extends EndlessScrollListener {
        @Override
        public void onLoadMore(int page, int totalItemsCount) {
            if (totalItemsCount >= pager.getTotalRow()) {
                return;
            }
            currentPage = page;
            updateView();
        }
    }

}
